<template>
	<view>
		<u-alert-tips type="warning" title="提示: 添加时请摸亮门锁密码键盘" :title-style="{textAlign: 'left'}" style="border: none;">
		</u-alert-tips>

		<uni-section class="mb-10" title="搜索中..."></uni-section>
		<uni-list>
			<uni-list-item :key="lock.deviceId" v-for="lock in lockList" :title="lock.deviceName" :note="lock.MAC"
				thumb="https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/unicloudlogo.png" thumb-size="base" >
				<template v-slot:footer>
					<view style="display: flex;align-items: center;">
						<uni-icons type="plus-filled" size="30" @click="addLock(lock)"></uni-icons>
					</view>
				</template>
			</uni-list-item>
		</uni-list>
	</view>
</template>

<script>
	import UniSection from "../../../components/uni-section/uni-section";
  import { toast } from '@/utils/common'
  import { initialize, deleteLock } from '@/api/device/lock'

  export default {
		components: {
			UniSection
		},
		data() {
			return {
				lockList: []
			}
		},
		methods: {
			startScan() {
				this.lockList = []
				requirePlugin("myPlugin").startScanBleDevice((lockDevice, lockList) => {
          console.log(lockList)
					this.lockList = lockList
				})
			},
			back() {
				this.$tab.navigateBack()
			},
			// 列表点击事件
			addLock(lock) {
				console.log(lock);
        this.handleInitLock(lock)
			},
      deleteLock(lock) {
        // deleteLock({
        //   lockId: lock.deviceId
        // }).then(res => {
        //   console.log(res)
        // })

        requirePlugin("myPlugin").resetLock(lock, (res) => {
          console.log(res)
        })
      },
      // 停止所有蓝牙操作，并退出操作中状态
      handleStopAllOperations() {
        this.lockList = []
      },
      handleInitLock(lockItem) {
        if (!lockItem.isSettingMode) {
          toast(`智能锁${lockItem.deviceName || lockItem.MAC}已被初始化，当前不可添加`)
          return
        }

        toast(`正在初始化蓝牙智能锁${lockItem.deviceName}, MAC地址：${lockItem.MAC}`)
        requirePlugin("myPlugin").initLock(lockItem, (res) => {
          console.log('设备初始化时连接断开', res)
        }).then(res => {
          console.log(res)
          if (res.errorCode === 0) {
            // 设备已成功初始化，请调用开放平台接口上传lockData
            toast('设备已成功初始化，正在调用开放平台接口上传lockData')
            initialize({
              lockData: res.lockData
            }).then(res => {
              if (res) {
                // TODO - 将锁写入数据库 & BUG
                wx.showToast({
                  icon: "success",
                  title: '智能锁已添加',
                  mask: true,
                  complete: () => {
                    setTimeout(this.$tab.navigateTo('/pages/device/lock/add-success'), 2000);
                  }
                })
              } else {
                toast("智能锁数据上传失败");
              }
            })
          } else {
            toast(`初始化智能锁失败, 错误信息：${res.errorMsg}`);
          }
        })
      },
		},
		mounted() {
			this.startScan()
		},
	}
</script>

<style lang="scss" scoped>
	/deep/.u-alert-tips {
		border: none !important;
	}
</style>
